<script>
import { useUserStore } from "./stores/user.js";
import { userinfo } from "@/services/user";
import { userloginStore } from "@/stores/islogin.js";
import { getOAuthUrl, wechat } from "@/services/login";
import { isMobile } from "@/utils/isMobile.js";
import loginto from "@/utils/reder.js";

import { Interceptfun } from "@/utils/intercept";
import { share, getUrlParams } from "@/utils/share.js";

export default {
  onLaunch: async function (e) {
    // #ifdef MP-WEIXIN
    let codes = "";
    if (e.query.scene) {
      if (e.query.hasOwnProperty("scene")) {
        console.log("scene", e.scene);
        switch (e.scene) {
          //扫描小程序码
          case 1047:
            codes = getUrlParams(decodeURIComponent(e.query.scene));
            console.log("codes", codes);

            break;
          //长按图片识别小程序码
          case 1048:
            codes = getUrlParams(decodeURIComponent(e.query.scene));
            break;
          //手机相册选取小程序码
          case 1049:
            codes = getUrlParams(decodeURIComponent(e.query.scene));
            break;
          //直接进入小程序
          case 1001:
            codes = getUrlParams(decodeURIComponent(e.query.scene));
            break;
        }
      }

      if (codes.referrer_id) {
        uni.setStorageSync(
          "referrer_id",
          codes.referrer_id ? codes.referrer_id : 0
        );
        if (!uni.getStorageSync("Authorization")) {
          if (!codes.id) {
            uni.navigateTo({
              url: "/pages/login/login?referrer_id=" + codes.referrer_id,
            });
          }
        }
      } else {
        if (uni.getStorageSync("referrer_id")) {
          uni.removeStorageSync("referrer_id");
        }
      }
    } else {
      if (e.query.referrer_id) {
        uni.setStorageSync(
          "referrer_id",
          e.query.referrer_id ? e.query.referrer_id : 0
        );
        if (!uni.getStorageSync("Authorization")) {
          if (!e.query.id) {
            uni.navigateTo({
              url: "/pages/login/login?referrer_id=" + e.query.referrer_id,
            });
          }
        }
      } else {
        if (uni.getStorageSync("referrer_id")) {
          uni.removeStorageSync("referrer_id");
        }
      }
    }
    // #endif

    // #ifdef H5
    var query = window && window.location.href.split("?")[1];
    var params = {};
    if (query) {
      var pairs = query.split("&");
      pairs.forEach(function (pair) {
        var parts = pair.split("=");
        params[decodeURIComponent(parts[0])] = decodeURIComponent(parts[1]);
      });
    }
    if (params.referrer_id) {
      uni.setStorageSync(
        "referrer_id",
        params.referrer_id ? params.referrer_id : 0
      );
      if (!uni.getStorageSync("Authorization")) {
        uni.navigateTo({
          url: "/pages/login/login?referrer_id=" + e.query.referrer_id,
        });
      }
    }
    // #endif

    // if (isMobile() && !e.code) {
    //   this.wcreders();
    // } else {
    // }
    // #ifdef H5 || APP-PLUS
    share(
      "明松旺",
      // encodeURIComponent(window.location.href.split("#")[0]),
      window.location.href,
      "https://m-cloud.yibaitao.com/60/2aac23d4403464e08cbb4bde11881c.jpg"
    );
    // #endif
    Interceptfun();
    if (uni.getStorageSync("Authorization")) {
      this.getuser();
    } else {
      // isMobile()?
      // if(isMobile()){
      //   this.wcreders();
      // }else{
      // }
    }
  },
  onPageNotFound: function (params) {
    uni.reLaunch({
      url: "/pages/index/index",
    });
  },
  onShow: function () {},
  onHide: function () {},
  methods: {
    getuser: async () => {
      const userlogin = userloginStore();
      const storesdata = useUserStore();
      let userdata = await userinfo();
      if ((userdata.code = 200)) {
        userlogin.setlogin(true);
      }

      storesdata.userInfo = userdata.data;
    },
    wcreders: async () => {
      let res = await getOAuthUrl({
        redirectUrl: location.href,
      });
      window.location.href = res.data.redirectUrl;
    },
  },
};
</script>

<style lang="scss">
@import "uview-plus/index.scss";
/*每个页面公共css */

// page {
//   font-family: "Medium";
// }
</style>
